package gbench.sandbox.plot.line;

import java.util.Arrays;
import java.util.stream.Stream;
import gbench.common.matlib.MatlibCanvas;
import gbench.common.matlib.MatlibCanvas.NVec;
import java.util.*;

public class JunitLine {

	public static class Canvas {
		public Canvas(int w, int h, int pz) {
			matlib = new MatlibCanvas(Math.max(w, h));
			this.w = w;
			this.h = h;
			this.pz = pz;
		}

		/**
		 * 
		 */
		public void flesh() {
			// 构建坐标系统
			matlib.plot(w, h, pen -> {
				intS(0, 1, w / pz).parallel().forEach(i -> {
					intS(0, 1, h / pz).parallel().forEach(j -> { // 计算像素坐标

						// 填充像素点
						intS(i * pz, 1, pz).forEach(_x -> {
							intS(j * pz, 1, pz).forEach(_y -> {

								if (i % 2 == 0 && j % 2 == 1)
									pen.drawPoint(NVec.of(_x, _y, 255, 255, 255));

								points.computeIfPresent(NVec.of(i, j), (k, v) -> {
									pen.drawPoint(NVec.of(_x, _y).concat(v.tail(3)));
									return v;
								});
							});
						});
					});
				});
			});
		}

		/**
		 * 
		 * @param n
		 * @return
		 */
		public static Stream<Integer> intS(int step) {
			return Stream.iterate(0, i -> i + step);
		}

		/**
		 * 
		 * @param n
		 * @return
		 */
		public static Stream<Integer> intS(int s, int step) {
			return Stream.iterate(s, i -> i + step);
		}

		/**
		 * 
		 * @param n
		 * @return
		 */
		public static Stream<Integer> intS(int s, int n, int size) {
			final var e = s + n * (size + 1);
			return Stream.iterate(s, i -> i < e, i -> i + n);
		}

		/**
		 * 
		 * @param <T>
		 * @param objs
		 */
		public static <T> void println(@SuppressWarnings("unchecked") T... objs) {
			System.out.println(Arrays.asList(objs));
		}

		/**
		 * 
		 * @param nvec
		 */
		public void drawPoint(NVec nvec) {
			points.put(nvec.head(2), nvec);
		}

		final int w;
		final int h;
		final int pz;
		final MatlibCanvas matlib;
		final Map<NVec, NVec> points = new HashMap<>();

	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		final var s = System.currentTimeMillis();
		final var canvas = new Canvas(1000, 600, 10);
		// 绘图图像
		Canvas.intS(1).takeWhile(i -> i < 100).forEach(x -> { // 绘制一条直线
			canvas.drawPoint(NVec.of(x, x, 0, 0, 255));
		});
		canvas.flesh();
		Canvas.println(new int[] { 1, 2 }.equals(new int[] { 1, 2 }));
		final var e = System.currentTimeMillis();
		System.out.println((e - s) / 1000d);
	}

}
